package com.xm.xmds1705083.service.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xm.xm.dto.ResultDto;
import com.xm.xm.dto.SearchDto;
import com.xm.xm.util.DateUtils;
import com.xm.xm.util.ErrorCode;
import com.xm.xmds1705083.service.mapper.DuoduoUserMapper;
import com.xm.xmds1705083.service.mapper.DuoduoVipConfigMapper;
import com.xm.xmds1705083.service.mapper.DuoduoVipRecordMapper;
import com.xm.xmds1705083.service.model.DuoduoUser;
import com.xm.xmds1705083.service.model.DuoduoVipConfig;
import com.xm.xmds1705083.service.model.DuoduoVipRecord;
import com.xm.xmds1705083.service.service.DuoduoVipConfigService;
import com.xm.xmds1705083.service.util.DateUtil;
import net.sf.json.JSONObject;
import org.apache.ibatis.session.RowBounds;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 杭州新苗网络科技有限公司
 *
 * @author meihf
 * @date 2017/06/16
 * @description
 */
@Service("duoduoVipConfigService")
public class DuoduoVipConfigServiceImpl extends ServiceImpl<DuoduoVipConfigMapper, DuoduoVipConfig> implements DuoduoVipConfigService {

    @Autowired
    DuoduoVipConfigMapper duoduoVipConfigMapper;

    @Autowired
    DuoduoUserMapper duoduoUserMapper;

    @Autowired
    DuoduoVipRecordMapper duoduoVipRecordMapper;

    protected Logger logger = LogManager.getLogger(getClass());

    @Override
    public ResultDto getMyVipInfo(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            JSONObject resultJson = new JSONObject();
            //查出会员的套餐
            RowBounds rowBounds = new RowBounds(searchDto.getPageNo(), searchDto.getPageSize());
            EntityWrapper entityWrapper = new EntityWrapper();
            entityWrapper.orderBy("date", true);
            List<DuoduoVipConfig> duoduoVipConfigList = duoduoVipConfigMapper.selectPage(rowBounds, entityWrapper);
            resultJson.put("vipList", duoduoVipConfigList);
            //先查我是否是会员以及会员是否到期 如果不是显示--
            DuoduoUser duoduoUser = duoduoUserMapper.selectList(new EntityWrapper<DuoduoUser>().
                    where(" id={0} and status=0 ", searchDto.getAppUserId())).get(0);
            int vipTime = duoduoUser.getVipTime();
            if (vipTime < searchDto.getTime()) {
                resultJson.put("myVip", "--");
                resultDto.setData(resultJson);
            } else {
                //查到套餐信息
                List<DuoduoVipConfig> vipConfigs = duoduoVipConfigList.stream().filter((duoduoVipConfig -> duoduoVipConfig.getId() == duoduoUser.getVipId())).collect(Collectors.toList());
                if (vipConfigs.size() == 0) {
                    logger.info("用户的vipid" + duoduoUser.getVipId() + "在vip表里没有对应值");
                } else {
                    resultJson.put("myVip", "￥" + vipConfigs.get(0).getPrice() + "/" + vipConfigs.get(0).getDate() + "月");
                }
                resultDto.setData(resultJson);
                resultDto.setMsg("查询成功！");
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

    @Override
    public ResultDto saveBuyInfo(SearchDto searchDto) {
        ResultDto resultDto = new ResultDto();
        JSONObject filed = searchDto.getFiled();
        searchDto.setPageNo((searchDto.getPageNo() - 1) * searchDto.getPageSize());
        try {
            if (!filed.has("vipId") || !filed.has("accountNo")) {
                resultDto.setMsg(ErrorCode.E10011);
                resultDto.setCode(ErrorCode.C10011);
                return resultDto;
            } else {
                String accountNo = filed.getString("accountNo");
                int vipId = filed.getInt("vipId");
                DuoduoVipRecord duoduoVipRecord = new DuoduoVipRecord();
                duoduoVipRecord.setAccountNo(accountNo);
                duoduoVipRecord.setUserId(searchDto.getAppUserId().intValue());
                duoduoVipRecord.setVipId(vipId);
                duoduoVipRecordMapper.insert(duoduoVipRecord);
                //查询套餐的信息 将个人信息里面vip时间改变
                DuoduoVipConfig duoduoVipConfig = duoduoVipConfigMapper.selectById(vipId);
                String time1 = accountNo.substring(0,accountNo.length()-4);
//                long time2 = DateUtils.getStringToLong(time1,DateUtils.PATTERN_DATE_TIME_SIMPLE);
                String vipTime = DateUtil.addMonth(3, DateUtils.getLongToString(Long.parseLong(time1),DateUtils.PATTERN_DATE_TIME));
                long time3 = Long.parseLong(vipTime.substring(0,vipTime.length()-3));
                DuoduoUser duoduoUser = duoduoUserMapper.selectById(searchDto.getAppUserId());
                duoduoUser.setVipTime((int) time3);
                duoduoUser.setIsVip(1);
                duoduoUser.setVipId(vipId);
                duoduoUserMapper.updateById(duoduoUser);
                //插入成功之后把vip信息返回
                JSONObject resultJson = new JSONObject();
                resultJson.put("myVip", "￥" + duoduoVipConfig.getPrice() + "/" + duoduoVipConfig.getDate() + "月");
                resultDto.setData(resultJson);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultDto.setCode(ErrorCode.C10013);
            resultDto.setMsg(ErrorCode.E10013);
        }
        return resultDto;
    }

    public static void main(String args[]) {
        long a = DateUtils.getTimeSecondLong();
        long b = DateUtils.getTimeMillisLong();
        System.out.println(a+"==========="+b);
        System.out.println(1506741071-1498792271);

        try {
//            long time2 = DateUtils.getStringToLong("20170630091921",DateUtils.PATTERN_DATE_TIME_SIMPLE);
            String c = DateUtil.addMonth(3,String.valueOf(DateUtils.getLongToString(1506734361000l,DateUtils.PATTERN_DATE_TIME)));
            System.out.println("c"+c);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

